package pku.ss.movieSystem.services.impl;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import pku.ss.movieSystem.common.ComPaginationSupport;
import pku.ss.movieSystem.common.Constants;
import pku.ss.movieSystem.services.ICommentsService;
import daos.Comment;
import daos.CommentDAO;
import daos.Movie;
import daos.MovieDAO;
import daos.User;
import daos.UserDAO;

public class CommentsService implements ICommentsService {
	   private CommentDAO commentDao;
	   private UserDAO userDao;
	   private MovieDAO movieDao;
	   
	   
       public ComPaginationSupport getCommentsByMovieId(int movieid,int startIndex){
    	   Integer movieId=new Integer(movieid);
    	 //  Movie movie=movieDao.findById(movieId);
    	   return commentDao.findCommentsByMovieId(movieId, startIndex, Constants.CPAGE_SIZE);
       }
       public List getComments(int movieId){
    	   Integer movieid=new Integer(movieId);
    	   Movie movie=movieDao.findById(movieid);
    	   List comments = commentDao.findByMovie(movie);
    	   Iterator it=comments.iterator();
    	   ArrayList contents = new ArrayList();
    	   while(it.hasNext())
    	   {
    		   Comment com=(Comment)it.next();
    		   contents.add(com.getContent());
    	   }
    	   return contents;
       }
     /*  public int getCommentsNum(int movieid){
    	   Integer movieId=new Integer(movieid);
    	   List clist=this.getCommentsByMovieId(movieId);
    	   return clist.size();
       }*/
       public void addComment(int movieId, int userId, String content){
    	   Calendar ca = Calendar.getInstance(); 
    	   Date now = ca.getTime();
    	   Integer movieid=new Integer(movieId);
    	   Movie movie=movieDao.findById(movieid);
    	   Integer userid=new Integer(userId);
    	   User user=userDao.findById(userid);
    	   Comment comment=new Comment();
    	   comment.setContent(content);
    	   comment.setComTimeStamp(now);
    	   comment.setMovie(movie);
    	   comment.setUser(user);
    	   commentDao.save(comment);
       }
      public void deleteComment(int commentId){
    	  Comment comment = commentDao.findById(commentId);
    	  commentDao.delete(comment);
      }
       public void setUserDao(UserDAO userDao) {
      		this.userDao = userDao;
        	}
       public UserDAO getUserDao() {
      		return this.userDao;
       	}
       public void setMovieDao(MovieDAO movieDao) {
   		this.movieDao = movieDao;
     	}
   	   public MovieDAO getMovieDao() {
   		return this.movieDao;
    	}
       public void setCommentDao(CommentDAO commentDao) {
   		this.commentDao = commentDao;
     	}
   	   public CommentDAO getCommentDao() {
   		return this.commentDao;
    	}
}
